package com.aj.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

import com.aj.activity.adapter.FamilyPhotoAdapter;
import com.aj.activity.adapter.FamilyPhotoAdapter.IOperationInterface;
import com.aj.activity.dialog.InfoDialog;
import com.aj.activity.dialog.InfoDialog.IInfoInterface;
import com.aj.application.LoveHomeApplication;
import com.aj.databases.dao.UserDao;
import com.aj.databases.entry.CmtInfo;
import com.aj.databases.entry.DigUserInfo;
import com.aj.databases.entry.LookPhotoInfo;
import com.aj.databases.entry.PhotoDateInfo;
import com.aj.databases.entry.PhotoInfo;
import com.aj.databases.entry.User;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.CMTRequest;
import com.aj.http.request.LookZonePhotoRequest;
import com.aj.http.request.PhotoReportRequest;
import com.aj.http.request.PraiseRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.http.response.LookZonePhotoResponse;
import com.aj.utils.Contants;

public class FamilyPhotoActivity extends BaseActivity implements
		IOperationInterface {
	private ListView familyphotolistview;
	private String pageFlag;
	private String zoneId;
	private String userId;
	private UserDao userDao;
	private User user;
	private List<PhotoInfo> photoInfoList;
	private FamilyPhotoAdapter familyPhotoAdapter;
	private PhotoInfo commonPhotoInfo;
	private CmtInfo cmtInfo = null;

	private EditText enter_pray_edittext;
	private Button expressionbutton;
	private Button sendbutton;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_family_photo);
		ShareSDK.initSDK(this);
		initView();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		ShareSDK.stopSDK(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		resume();
	}

	private void initView() {
		zoneId = getIntent().getStringExtra(Contants.ZONE_ID_PARAMS);
		userId = getIntent().getStringExtra(Contants.USER_ID_PARAMS);
		userDao = new UserDao(this);
		user = userDao.getCurrentUser(LoveHomeApplication.token);
		familyphotolistview = (ListView) findViewById(R.id.familyphotolistview);
		pageFlag = getIntent().getStringExtra(Contants.PAGEFLAG_STRING);
		setActionBarTitle(R.string.person_photo_text);
		createMenu();
	}

	private void resume() {
		showProgressDialog();
		new LookZonePhotoRequest().excute(FamilyPhotoActivity.this,
				user.getUser_id(), zoneId, 50, 0, new IHttpCallBack() {
					@Override
					public void success(String result) {
						try {

							LookZonePhotoResponse response = new LookZonePhotoResponse(
									new JSONObject(result));
							Common common = response.getCommon();
							if (common != null
									&& common
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
								LookPhotoInfo lookPhotoInfo = response
										.getLookPhotoInfo();
								List<PhotoDateInfo> photoDateInfoList = lookPhotoInfo
										.getPhotoDateInfoList();
								if (photoDateInfoList != null
										&& photoDateInfoList.size() > 0) {
									photoInfoList = new ArrayList<PhotoInfo>();
									for (PhotoDateInfo photoDateInfo : photoDateInfoList) {
										List<PhotoInfo> tempPhoneInfoList = photoDateInfo
												.getPhotoInfoList();
										if (tempPhoneInfoList != null
												&& tempPhoneInfoList.size() > 0) {
											for (PhotoInfo phoneInfo : tempPhoneInfoList) {
												phoneInfo.setDate(photoDateInfo
														.getDate());
												photoInfoList.add(phoneInfo);
											}
											familyPhotoAdapter = new FamilyPhotoAdapter(
													FamilyPhotoActivity.this,
													photoInfoList,
													FamilyPhotoActivity.this);
											familyphotolistview
													.setAdapter(familyPhotoAdapter);
										}
									}
								}
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							dimissProgressDialog();
						}
					}

					@Override
					public void fail(String result) {
						dimissProgressDialog();
					}
				});
	}

	@Override
	public void shared(PhotoInfo photoInfo) {
		showShare(photoInfo);
	}

	private void showShare(PhotoInfo photoInfo) {
		OnekeyShare oks = new OnekeyShare();
		// 分享时Notification的图标和文字
		// oks.setNotification(R.drawable.ic_launcher,
		// getString(R.string.app_name));
		oks.setTitle(getString(R.string.app_name));
		oks.setText("爱家APP，让你更快更便捷的了解家人动向~");
		oks.setImageUrl(photoInfo.getPhoto());
		// 启动分享GUI
		oks.show(this);
	}

	@Override
	public void report(PhotoInfo photoInfo) {
		showProgressDialog();
		new PhotoReportRequest().excute(this, user.getUser_id(),
				photoInfo.getPhotoId(), new IHttpCallBack() {

					@Override
					public void success(String result) {
						try {
							Common common = new Common(new JSONObject(result));
							Toast.makeText(FamilyPhotoActivity.this,
									common.getErrorMsg(), Toast.LENGTH_LONG)
									.show();
						} catch (JSONException e) {
							e.printStackTrace();
						}
						dimissProgressDialog();
					}

					@Override
					public void fail(String result) {
						dimissProgressDialog();
						Toast.makeText(FamilyPhotoActivity.this, "网络异常",
								Toast.LENGTH_LONG).show();
					}
				});
	}

	@Override
	public void praise(final PhotoInfo photoInfo,
			final CompoundButton buttonView, final boolean isChecked,
			final TextView parsertext, final ImageView imageView) {
		showProgressDialog();
		String oper = "0";
		if (isChecked) {
			oper = "0";
		} else {
			oper = "1";
		}
		new PraiseRequest().excute(FamilyPhotoActivity.this, user.getUser_id(),
				photoInfo.getPhotoId(), oper, new IHttpCallBack() {
					@Override
					public void success(String result) {
						try {
							Common common = new Common(new JSONObject(result));
							if (common.getReturnCode() == null
									|| !common
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
								buttonView.setChecked(!isChecked);
							} else {
								List<DigUserInfo> digUserInfoList = photoInfo
										.getDigUserInfoList();
								if (isChecked) {
									if (digUserInfoList == null) {
										digUserInfoList = new ArrayList<DigUserInfo>();
									}
									DigUserInfo digUserInfo = new DigUserInfo();
									digUserInfo.setDig_user_id(user
											.getUser_id());
									digUserInfo.setDig_user_nick(user
											.getNickName() == null ? "" : user
											.getNickName());
									digUserInfoList.add(digUserInfo);
									photoInfo.setHadDig("1");
								} else {
									for (DigUserInfo digUserInfo : digUserInfoList) {
										if (digUserInfo.getDig_user_id()
												.equals(user.getUser_id())) {
											digUserInfoList.remove(digUserInfo);
											break;
										}
									}
									photoInfo.setHadDig("0");
								}
								if (digUserInfoList == null
										|| digUserInfoList.size() == 0) {
									parsertext.setVisibility(View.INVISIBLE);
									imageView.setVisibility(View.INVISIBLE);
								} else {
									String text = "";
									for (DigUserInfo digUserInfo : digUserInfoList) {
										text += digUserInfo.getDig_user_nick()
												+ ",";
									}
									text = text.substring(0,
											text.lastIndexOf(","));
									parsertext.setVisibility(View.VISIBLE);
									imageView.setVisibility(View.VISIBLE);
									parsertext.setText(text + "觉得很赞");
								}
							}
							Toast.makeText(FamilyPhotoActivity.this,
									common.getErrorMsg(), Toast.LENGTH_LONG)
									.show();
						} catch (JSONException e) {
							e.printStackTrace();
							Toast.makeText(FamilyPhotoActivity.this, "网络异常",
									Toast.LENGTH_LONG).show();
							buttonView.setChecked(!isChecked);
						}
						dimissProgressDialog();

					}

					@Override
					public void fail(String result) {
						dimissProgressDialog();
						Toast.makeText(FamilyPhotoActivity.this, "网络异常",
								Toast.LENGTH_LONG).show();
					}
				});
	}

	@Override
	public void commont(PhotoInfo photoInfo, CmtInfo cmtInfo) {
		this.commonPhotoInfo = photoInfo;
		this.cmtInfo = cmtInfo;
		openPanel();
	}

	private void createMenu() {
		View view = LayoutInflater.from(FamilyPhotoActivity.this).inflate(
				R.layout.menu_common_enter, null);
		// ClickEvent clickEvent = new ClickEvent();
		enter_pray_edittext = (EditText) view
				.findViewById(R.id.enter_pray_edittext);
		expressionbutton = (Button) view.findViewById(R.id.expressionbutton);
		sendbutton = (Button) view.findViewById(R.id.sendbutton);
		sendbutton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (enter_pray_edittext.getText().toString().trim().length() == 0) {
					Toast.makeText(FamilyPhotoActivity.this, "请输入评论内容~",
							Toast.LENGTH_LONG).show();
					return;
				}
				showProgressDialog();
				if (cmtInfo == null) {
					new CMTRequest().excute(FamilyPhotoActivity.this,
							user.getUser_id(), "1", null, "1",
							enter_pray_edittext.getText().toString(),
							commonPhotoInfo.getPhotoId(), null,
							user.getPhohe(), user.getPhohe(),
							new IHttpCallBack() {
								@Override
								public void success(String result) {
									try {
										closePanel();
										Common common = new Common(
												new JSONObject(result));
										if (common.getReturnCode() != null
												&& common
														.getReturnCode()
														.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
											resume();
										}
										Toast.makeText(
												FamilyPhotoActivity.this,
												common.getErrorMsg(),
												Toast.LENGTH_LONG).show();
									} catch (JSONException e) {
										e.printStackTrace();
									}
									dimissProgressDialog();
								}

								@Override
								public void fail(String result) {
									closePanel();
									dimissProgressDialog();
									Toast.makeText(FamilyPhotoActivity.this,
											"网络异常", Toast.LENGTH_LONG).show();
								}
							});

				} else {
					new CMTRequest().excute(FamilyPhotoActivity.this,
							user.getUser_id(), "1", null, "1",
							enter_pray_edittext.getText().toString(),
							commonPhotoInfo.getPhotoId(),
							cmtInfo.getCommonId(), user.getPhohe(),
							user.getPhohe(), new IHttpCallBack() {
								@Override
								public void success(String result) {
									try {
										closePanel();
										Common common = new Common(
												new JSONObject(result));
										if (common.getReturnCode() != null
												&& common
														.getReturnCode()
														.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
											resume();
										}
										Toast.makeText(
												FamilyPhotoActivity.this,
												common.getErrorMsg(),
												Toast.LENGTH_LONG).show();
									} catch (JSONException e) {
										e.printStackTrace();
									}
									dimissProgressDialog();
								}

								@Override
								public void fail(String result) {
									closePanel();
									dimissProgressDialog();
									Toast.makeText(FamilyPhotoActivity.this,
											"网络异常", Toast.LENGTH_LONG).show();
								}
							});
				}
			}
		});

		setMenuLayout(view);
	}

	@Override
	public void commontDelete(PhotoInfo photoInfo, final CmtInfo cmtInfo) {
		if (user.getUser_id().equals(cmtInfo.getCmtUserId())) {
			InfoDialog infoDialog = new InfoDialog(this);
			infoDialog.show();
			infoDialog.setContent("是否真的删除自己的评论~");
			infoDialog.setInfoInterface(new IInfoInterface() {
				@Override
				public void sureInfo() {
					showProgressDialog();
					new CMTRequest().excute(FamilyPhotoActivity.this,
							user.getUser_id(), "1", cmtInfo.getCommonId(), "3",
							cmtInfo.getCmtContent(), null, null,
							user.getPhohe(), user.getPhohe(),
							new IHttpCallBack() {
								@Override
								public void success(String result) {
									try {
										dimissProgressDialog();
										Common common = new Common(
												new JSONObject(result));
										Toast.makeText(
												FamilyPhotoActivity.this,
												common.getErrorMsg(),
												Toast.LENGTH_LONG).show();
										if (common.getReturnCode() != null
												&& common
														.getReturnCode()
														.trim()
														.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
											resume();
										}
									} catch (JSONException e) {
										e.printStackTrace();
									}
								}

								@Override
								public void fail(String result) {
									try {
										dimissProgressDialog();
										Common common = new Common(
												new JSONObject(result));
										Toast.makeText(
												FamilyPhotoActivity.this,
												common.getErrorMsg(),
												Toast.LENGTH_LONG).show();
									} catch (JSONException e) {
										e.printStackTrace();
									}
								}
							});
				}

				@Override
				public void cannelInfo() {
					// TODO Auto-generated method stub

				}
			});
		}
	}
}
